//
// Created by benxb on 2021/12/11.
//

#ifndef BENXDB_LOG_MANAGER_H
#define BENXDB_LOG_MANAGER_H

#include <fstream>
#include <common/config.h>
#include "mem_log.h"
#include "tuple.h"
#include <catelog/table.h>
#include <memory>
#include <catelog/point_cloud.h>
#include <catelog/point_type.h>

namespace benxdb {

class LogManager {
public:
    explicit LogManager();
    explicit LogManager(const std::string& table_name, lsn_t lsn);
    ~LogManager();

    /**
     * Append new log into file
     * log file only record insert
     * @param tuple
     * @return false if read only
     */
    bool AppendLog(Tuple& tuple);

    bool AppendManyLog(std::vector<Tuple>& tuple);

    bool AppendManyLog(std::vector<Tuple>& tuple, int begin, int end);

    bool AppendManyLog(PointCloud<PointXYZI>& point_cloud, int begin, int end);

    bool AppendManyLog(PointCloud<PointSTXYZ>& point_cloud, int begin, int end);

    bool AppendManyLog(PointCloud<PointSTXYZC>& point_cloud, int begin, int end);

    bool AppendPointLog(const char* point, int size);

    void ChangeReadOnly();

    /**
     * Read all tuple
     * @param log
     */
    void ReadAllLog(const std::shared_ptr<Table>& table, std::vector<Tuple>& tuple);

    void DeleteLogFile();

private:
    lsn_t lsn_;
    // stream to write db file
    std::fstream log_io_;
    std::string log_file_;
    bool read_only_ = false;
};

}


#endif //BENXDB_LOG_MANAGER_H
